package com.paulianer.fifa;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class Start extends JFrame implements NativeKeyListener {
	private static final long serialVersionUID = 1L;
	private JTextArea txtEventInfo;
	private INT_Team team;
	private Robot robot;
	
	public static void main(String[] args) {
		new Start();
	}
	
	public Start() {
		setTitle("Fifa");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600, 300);
				
		txtEventInfo = new JTextArea();
		txtEventInfo.setEditable(false);
		txtEventInfo.setBackground(new Color(0xFF, 0xFF, 0xFF));
		txtEventInfo.setForeground(new Color(0x00, 0x00, 0x00));
		txtEventInfo.setText("");
		
		JScrollPane scrollPane = new JScrollPane(txtEventInfo);
		scrollPane.setPreferredSize(new Dimension(375, 125));
		add(scrollPane);
		
		setVisible(true);
		try {
			GlobalScreen.registerNativeHook();
			robot = new Robot();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		GlobalScreen.getInstance().addNativeKeyListener(this);
		
		team = new TEAM_BVB();
	}
	
	private static void keypress(int key, Robot robot) {
		sleep();
		robot.keyPress(key);
		sleep();
		robot.keyRelease(key);
	}
	
	private void record(String c) {
		System.out.print(c);
	}
	
	private void equalKey(char c) {
		if(this.team.key() == c) {
			displayEventInfo("check");
			for(int i = 0; i < team.upDown().length(); i++) {
				switch(this.team.upDown().charAt(i)) {
				case 'r': 
					robot.keyPress(KeyEvent.VK_RIGHT);
					break;
				case 'u': 
					robot.keyPress(KeyEvent.VK_UP);
					break;
				case 'd':
					robot.keyPress(KeyEvent.VK_DOWN);
					break;
				case 'e':
					robot.keyPress(KeyEvent.VK_ENTER);
					break;
				case 'k':
					robot.keyPress(KeyEvent.VK_ESCAPE);
					break;
				}
				sleep();
			}
		}
	}
	
	private static  void sleep() {
		try {Thread.sleep(1000);} catch (Exception e) {}
	}
	
	private void displayEventInfo(final String e) {
		/* Note: JNativeHook *CANNOT* operate on the event dispatch thread.
		 * Because Swing components must be accessed on the event dispatching
		 * thread, you *MUST* wrap access to Swing components using the
		 * SwingUtilities.invokeLater() or EventQueue.invokeLater() methods.
		 */
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				txtEventInfo.append("\n" + e);
			}
		});
	}
	
	@Override
	public void nativeKeyPressed(NativeKeyEvent arg0) {
		// TODO Auto-generated method stub
		equalKey((char)arg0.getKeyCode());
		displayEventInfo(arg0.paramString());
	}

	@Override
	public void nativeKeyReleased(NativeKeyEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void nativeKeyTyped(NativeKeyEvent arg0) {
		// TODO Auto-generated method stub
	}
}
